#include <iostream>

#include <unistd.h>

#include "pthread.h"

#include "Blockqueue.hpp"




void *ProducerRoutine(void * arg)
{
    BlockQueueModule::BlockQueue<int> *bq = static_cast<BlockQueueModule::BlockQueue<int>*>(arg);

    int data = 10;
    while(true)
    {
        // sleep(2);
        
        bq->Enqueue(data);
        std::cout << "produce a data: "<< data << std::endl;
        ++data;
    }

}

void *ConsumerRoutine(void * arg)
{
    BlockQueueModule::BlockQueue<int> *bq = static_cast<BlockQueueModule::BlockQueue<int>*>(arg);

    int data = 0;
    while(true)
    {
        sleep(2);
        bq->Dequeue(&data);
        std::cout << "get a data: " << data << std::endl;

    }
}

int main()
{
    BlockQueueModule::BlockQueue<int> bq;

    pthread_t producer1, consumer1;

    // 多生产者多消费者直接加线程
    pthread_t p2,p3,p4;
    pthread_t c1,c2;

    pthread_create(&producer1, nullptr, ProducerRoutine, (void*)&bq);
    pthread_create(&consumer1, nullptr, ConsumerRoutine, (void*)&bq);

    pthread_create(&p2, nullptr, ProducerRoutine, (void*)&bq);
    pthread_create(&p3, nullptr, ProducerRoutine, (void*)&bq);
    pthread_create(&p4, nullptr, ProducerRoutine, (void*)&bq);

    pthread_create(&c1, nullptr, ConsumerRoutine, (void*)&bq);
    pthread_create(&c2, nullptr, ConsumerRoutine, (void*)&bq);

    pthread_join(consumer1, nullptr);
    pthread_join(producer1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    pthread_join(p4, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);

    return 0;
}